<template>
  <div
    v-for="(row, rowIdx) in list"
    :key="rowIdx"
    v-drag-scroll
    class="row"
    :class="'row' + rowIdx"
  >
    <div
      v-for="(item, itemIdx) in row"
      :key="itemIdx"
      class="item"
      :class="item.selected ? 'selected' : ''"
      @click="editItem(item, row)"
    >
      <div class="item-t1">{{ item.type }} [{{ item.slot }}]</div>

      <div class="item-t2-wrap">
        <div
          v-for="(property, idx) in itemProperties(item)"
          :key="idx"
          class="item-t2"
        >
          <div class="l">
            {{ property.name }}
            <span v-if="property.add > 1" class="add-icon">
              {{ propertyAddIcon(property.add) }}
            </span>
          </div>

          <div class="r">
            {{ property.valueStr }}
          </div>
        </div>
      </div>

      <div class="item-t3">
        {{ item['总分'] }}
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    props: {
      list: {
        type: Array,
        required: true,
        default: () => {
          return []
        },
      },
    },

    emits: ['edit'],

    data() {
      return {
        idxItem: null,
      }
    },

    mounted() {},

    methods: {
      toNumber(val) {
        // 转为数字 四舍五入保留1位小数
        val = Math.round(val * 10) / 10
        return Number(val)
      },

      // 返回item需要显示的属性
      itemProperties(item) {
        let map = [
          '暴击率',
          '暴击伤害',
          '生命值',
          '攻击力',
          '小攻击',
          '小生命',
          '以太伤害',
        ]
        let valueBase = {
          暴击率: 2.4,
          暴击伤害: 4.8,
          生命值: 6,
          攻击力: 3,
          小攻击: 19,
          小生命: 112,
          以太伤害: 3,
        }
        let list = []
        for (let key of map) {
          if (!item[key] && item[key] !== 0) {
            continue
          }
          let value = this.toNumber(valueBase[key] * item[key])
          list.push({
            name: key,
            add: item[key],
            value: value,
            valueStr: ['小攻击', '小生命'].includes(key) ? value : `${value}%`,
          })
        }
        return list
      },

      editItem(item, row) {
        for (let i of row) {
          i.selected = false
        }

        item.selected = true
        this.$emit('edit', item)
      },

      propertyAddIcon(add) {
        if (add < 1) {
          return ''
        }
        if (add === 10) {
          return '10'
        }
        return `+${add - 1}`
      },
    },
  }
</script>

<style scoped>
  .row {
    display: flex;
    gap: 15px;
    flex-shrink: 0;
    width: 100%;
    overflow-x: auto;
    padding: 15px;

    .item {
      flex-shrink: 0;

      width: 180px;
      background-color: #1a1a1a;
      padding: 10px;
      border-radius: 5px;
      display: flex;
      flex-direction: column;
      gap: 10px;

      &.selected {
        border: 4px solid #16d6e4;
      }

      .item-t1 {
        font-size: 20px;
        font-weight: bold;
        color: #fff;
      }

      .item-t2-wrap {
        display: flex;
        flex-direction: column;
        gap: 10px;
        height: 114px;
      }
      .item-t2 {
        color: #d79b39;
        font-size: 16px;

        display: flex;
        justify-content: space-between;
        align-items: flex-end;

        .add-icon {
          background-color: #d79b39;
          color: #161817;
          border-radius: 2px;
          font-size: 14px;
          padding: 0 4px;
          margin-left: 4px;
        }
      }
      .item-t3 {
        font-size: 18px;
        font-weight: bold;

        color: #de1a79;
      }
    }
  }

  .row0 {
    display: none;
  }
</style>
